#encoding: utf-8
module Console
  def get_input(options={})
    reject_massage = options[:reject_massage] || '输入不正确，请重新输入：'
    allows = options[:allows].map { |i| i.to_s } if options[:allows]

    while input = STDIN.gets.strip
      break if input.empty? && options[:allow_empty]     #允许回车
      if allows && allows.length > 0                     #如果有限定输入
        if allows.include? input
          break
        else
          puts reject_massage.yellow unless input.empty?
        end
      elsif !input.empty?                               #没有限定输入，输入不正确不为空
        break
      end

    end

    input
  end

  def puts(message, color = nil)
    Dir.mkdir("#{LOG_PATH}") unless Dir.exists?("#{LOG_PATH}")
    logfile = open_log_file
    logfile << "#{message.to_s}\n"
    super("#{message.to_s}".colorize(color: color))
    logfile.close
  end

  def puts_blue message
    puts message, :blue
  end

  def puts_yellow message
    puts message, :yellow
  end

  def puts_error message
    puts message, :red
  end

  def puts_green message
    puts message, :green
  end

  def open_log_file
    File.open("#{LOG_PATH}/track.log", File::WRONLY | File::APPEND | File::CREAT)
  end
end
